#include <jni.h>
#include <string>
#include "include/eXosip2/eXosip.h"
#include <android/log.h>
#include "include/common.h"
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include "include/ctrl.h"
#include  <sys/socket.h>
#include "include/refresh.h"
#include "include/gb_lib.h"
extern "C" {
DEVICE_INFO device_info;
DEVICE_STATUS device_status;
DEVICE_CONTROL device_control;
DT_EXOSIP_CALLBACK dt_eXosip_callback;
BOOL SenAliveFlag = FALSE;
int g_call_id = 0;/*INVITE连接ID/用来分辨不同的INVITE连接，每个时刻只允许有一个INVITE连接*/
int g_register_id = 0;/*注册ID/用来更新注册或取消注册*/
int g_did_realPlay = 0;/*会话ID/用来分辨不同的会话：实时视音频点播*/
int g_did_backPlay = 0;/*会话ID/用来分辨不同的会话：历史视音频回放*/
int g_did_fileDown = 0;/*会话ID/用来分辨不同的会话：视音频文件下载*/

pthread_t pthread;
void* normalCallBack(void* data){


    char eXosip_server_id[30] = "34020000002000000001";
    char eXosip_server_ip[20] = "10.39.62.3";
    char eXosip_server_port[10] = "5060";
    char eXosip_ipc_id[30] = "34020000001310000010";
    char eXosip_ipc_pwd[20] = "123456";
    char eXosip_ipc_ip[20] = "10.2.114.253";
    char eXosip_ipc_media_port[10] = "20000";
    char eXosip_ipc_sess_port[10] = "5080";
    char eXosip_alarm_id[30] = "34020000001340000010";

    char eXosip_media_ip[30] = "10.2.114.253";
    char eXosip_media_port[10] = "6000";

    char eXosip_device_name[30] = "aname";
    char eXosip_device_manufacturer[30] = "datang";
    char eXosip_device_model[30] = "amodel";
    char eXosip_device_firmware[30] = "V1.0";
    char eXosip_device_encode[10] = "ON";
    char eXosip_device_record[10] = "OFF";

    char eXosip_status_on[10] = "ON";
    char eXosip_status_ok[10] = "OK";
    char eXosip_status_online[10] = "ONLINE";
    char eXosip_status_guard[10] = "OFFDUTY";
    char eXosip_status_time[30] = "2014-01-17T16:30:20";


    device_info.server_id = eXosip_server_id;
    device_info.server_ip = eXosip_server_ip;
    device_info.server_port = eXosip_server_port;
    device_info.ipc_id = eXosip_ipc_id;
    device_info.ipc_pwd = eXosip_ipc_pwd;
    device_info.ipc_ip = eXosip_ipc_ip;
    device_info.ipc_media_port = eXosip_ipc_media_port;
    device_info.ipc_sess_port = eXosip_ipc_sess_port;

    device_info.alarm_id = eXosip_alarm_id;

    device_info.media_ip = eXosip_media_ip;
    device_info.media_port = eXosip_media_port;

    device_info.device_name = eXosip_device_name;
    device_info.device_manufacturer = eXosip_device_manufacturer;
    device_info.device_model = eXosip_device_model;
    device_info.device_firmware = eXosip_device_firmware;
    device_info.device_encode = eXosip_device_encode;
    device_info.device_record = eXosip_device_record;

    device_status.status_on = eXosip_status_on;
    device_status.status_ok = eXosip_status_ok;
    device_status.status_online = eXosip_status_online;
    device_status.status_guard = eXosip_status_guard;
    device_status.status_time = eXosip_status_time;

    dt_eXosip_callback.dt_eXosip_deviceControl = dt_eXosip_deviceControl;
    dt_eXosip_callback.dt_eXosip_getDeviceInfo = dt_eXosip_getDeviceInfo;
    dt_eXosip_callback.dt_eXosip_getDeviceStatus = dt_eXosip_getDeviceStatus;
    dt_eXosip_callback.dt_eXosip_getRecordTime = dt_eXosip_getRecordTime;
    dt_eXosip_callback.dt_eXosip_mediaControl = dt_eXosip_mediaControl;
    dt_eXosip_callback.dt_eXosip_playControl = dt_eXosip_playControl;

    dt_eXosip_rigister();

    pthread_exit(NULL);
}
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_ndk_12_13_12_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    pthread_create(&pthread, NULL, normalCallBack, (void *) 1);
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_ndk_12_13_12_MainActivity_callTestFromJNI(JNIEnv *env, jobject thiz) {
    int retPlay = dt_eXosip_playControl(NULL, NULL, NULL, NULL, NULL);
    char retStr[100];
    snprintf(retStr, sizeof(retStr), "ret:%d", retPlay);
    return env->NewStringUTF(retStr);
}